//
//  SmallDecisionsMyViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/8.
//

#import "SmallDecisionsMyViewController.h"
#import "SmallDecisionsVipViewController.h"
#import "SmallDecisionsWebViewController.h"
#import "SmallDecisionsSingletonHelper.h"

@interface SmallDecisionsMyCell : UITableViewCell
@property (nonatomic, strong) UIView *containView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *iconImageView;

@end

@implementation SmallDecisionsMyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIView *selectedBackgroundView = [[UIView alloc] init];
        selectedBackgroundView.backgroundColor = [UIColor clearColor];
        self.selectedBackgroundView = selectedBackgroundView;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        [self createSmallDecisionsMyCellSubViews];
    }
    return self;
}

- (void)createSmallDecisionsMyCellSubViews {
    UIImageView *iconImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:iconImageView];
    self.iconImageView = iconImageView;
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_offset(28);
    }];
    

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = SmallDecisionsSetFontSize(16);
    titleLabel.numberOfLines = 0;
    titleLabel.textColor = SmallDecisionsThemeFontColor;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(10);
        make.centerY.equalTo(self.contentView);
    }];
    
    UIImageView *arrowImageView = [[UIImageView alloc] init];
    arrowImageView.contentMode = UIViewContentModeScaleAspectFill;
    arrowImageView.image = [UIImage imageNamed:@"global_arrow_right"];
    [self.contentView addSubview:arrowImageView];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-16);
        make.width.mas_offset(5);
        make.height.mas_offset(10);
        make.centerY.equalTo(self.contentView);
    }];
}

@end

@interface SmallDecisionsMyViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *tableList;


@end

@implementation SmallDecisionsMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;

    self.title = @"设置";
    self.tableList = @[
        @{@"name":@"分享应用",@"icon":@"my_share_img"},
        @{@"name":@"隐私政策",@"icon":@"my_prototcl_img"},
        @{@"name":@"关于我们",@"icon":@"my_aboutUs_img"}];
    [self myTableView];
}

#pragma markUITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SmallDecisionsMyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SmallDecisionsMyCellId" forIndexPath:indexPath];
    NSDictionary *dic = self.tableList[indexPath.row];
    cell.titleLabel.text = dic[@"name"];
    cell.iconImageView.image = [UIImage imageNamed:dic[@"icon"]];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        [SmallDecisionsSingletonHelper shareAppToPeople];
    } else if(indexPath.row == 1){
        [SmallDecisionsSingletonHelper openPrivacyProtocalWebView];
    } else {
        [SmallDecisionsSingletonHelper openAboutUsWebView];
    }
}

- (UITableView *)myTableView {
    if(!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.rowHeight = 62;
        _myTableView.tableHeaderView = [self createTableViewHeaderView];
        _myTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _myTableView.separatorColor = SmallDecisionsLineHeightColor;
        _myTableView.backgroundColor = [UIColor clearColor];
        [_myTableView registerClass:[SmallDecisionsMyCell class] forCellReuseIdentifier:@"SmallDecisionsMyCellId"];
        [self.view addSubview:_myTableView];
        [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo(0);
        }];
    }
    return _myTableView;
}

- (UIView *)createTableViewHeaderView {
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth * 0.4 + 20)];
    UIButton *headerViewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [headerViewBtn setBackgroundImage:[UIImage imageNamed:@"my_top_img"] forState:UIControlStateNormal];
    [headerViewBtn addTarget:self action:@selector(tapTableViewHeaderViewAction) forControlEvents:UIControlEventTouchUpInside];
    [tableHeaderView addSubview:headerViewBtn];
    [headerViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.bottom.mas_offset(0);
        make.left.bottom.mas_offset(-2);
        make.right.mas_offset(2);
    }];
    
    return tableHeaderView;
}

- (void)tapTableViewHeaderViewAction {
    SmallDecisionsVipViewController *vipVC = [[SmallDecisionsVipViewController alloc] init];
    [SmallDecisionsNavigatorManager pushViewController:vipVC];
}

@end

